package com.kele.goods.controller;

import com.kele.api.goods.CategoryControllerApi;
import com.kele.common.model.response.Result;
import com.kele.goods.service.CategoryService;
import com.kele.model.goods.Category;
import com.kele.model.goods.dto.CategoryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
//@CrossOrigin
public class CategoryController implements CategoryControllerApi {


    @Autowired
    CategoryService categoryService;

    @GetMapping("findAll")
    public List<Category> findAll(){
        return categoryService.findAll();
    }

    @GetMapping("findById/{id}")
    public Category findById(@PathVariable Long id) {
        return categoryService.findById(id);
    }

    @GetMapping("findByParentId/{parentId}")
    public List<Category> findByParentId(@PathVariable Long parentId) {
        return categoryService.findByParentId(parentId);
    }

    @GetMapping("findCategoryDTO")
    public List<CategoryDTO> findCategoryDTO(){
        return categoryService.findCategoryDTO();
    }

    @GetMapping("listTree")
    public List<Category> findCategoryTree(){
        return categoryService.findCategoryTree();
    }

    @Override
    @PostMapping("/save")
    public Result save(@RequestBody Category category) {
        if(category!=null){
            if(category.getId()==null){
                categoryService.add(category);
                return new Result(true,"1","新增成功");
            }else {
                categoryService.update(category);
                return new Result(true,"1","更新成功");
            }
        }
        return Result.FAIL();
    }

    @GetMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        return categoryService.deleteById(id);
    }

    @GetMapping("deleteByIdCascade/{id}")
    public Result deleteByIdCascade(@PathVariable Long id){
        return categoryService.deleteByIdCascade(id);
    }

}
